﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Text;

namespace Invoicing.AJAX
{
    /// <summary>
    /// MenuTree 的摘要说明
    /// </summary>
    public class MenuTree : IHttpHandler
    {
        HttpRequest request = HttpContext.Current.Request;

        public void ProcessRequest(HttpContext context)
        {

            try
            {

                if (request["type"] != null)
                {
                     // 1000 获取全部未选中的权限
                     // 999  获取除了角色权限外的权限
                     // 880  根据角色获取角色权限
                     // 773  根据用户获取扩展权限
                    ZY.BLL.Tab_Power powerdal = new ZY.BLL.Tab_Power();
                    ZY.BLL.Tab_RolePower roledal = new ZY.BLL.Tab_RolePower();
                    StringBuilder buff = new StringBuilder();
                    DataTable dspoall = powerdal.GetAllList().Tables[0];

                    switch (request["type"].ToString())
                    {

                        case "1000":
                            #region 获取全部菜单权限
                                  //DataTable dspower = powerdal.GetAllList().Tables[0];
                                  //buff.Append("{\"treedata\":[");
                                  //foreach (DataRow item in dspower.Rows)
                                  //  {
                                  //      buff.Append("{");
                                  //      buff.Append("\"id\":" + item["po_id"].ToString() + ",");
                                  //      buff.Append("\"pId\":" + item["po_upid"].ToString() + ",");
                                  //      buff.Append("\"name\":\"" + item["po_name"].ToString() + "\",");
                                  //      buff.Append("\"open\":false,");
                                  //      buff.Append("\"checked\":false");
                                  //      buff.Append("},");
                                  //  }
                                  //  string tempall = buff.ToString().TrimEnd(',') + "]}";
                                    HttpContext.Current.Response.ContentType = "text/plain";
                                    HttpContext.Current.Response.Write(JsonDataForRoleRight());
                            #endregion
                            break;
                        case  "999":
                            #region 设置所选角色的为选中的权限
                            if (request["rid"]!=null)
                            {
                               //获取角色的权限

                                DataSet  dsrole = roledal.GetList("rid="+request["rid"].ToString());
                                if (dsrole.Tables[0].Rows.Count > 0)
                                {
                                    DataTable dtrole = dsrole.Tables[0];
                                    string[] ridlist = dtrole.Rows[0]["rp_powerContext"].ToString().Split(',');
                                     //loop the datatable 

                                    buff.Append("{\"treedata\":[");
                                    foreach (DataRow item in dspoall.Rows)
                                    {
                                        string ischeck = "false";
                                        for (int i = 0; i < ridlist.Length; i++)
                                        {
                                            if (item["po_id"].ToString() == ridlist[i].ToString())
                                            {
                                                ischeck = "true";
                                                break;
                                            }
                                        }
                                        buff.Append("{");
                                        buff.Append("\"id\":" + item["po_id"].ToString() + ",");
                                        buff.Append("\"pId\":" + item["po_upid"].ToString() + ",");
                                        buff.Append("\"name\":\"" + item["po_name"].ToString() + "\",");
                                        buff.Append("\"open\":false,");
                                        buff.Append("\"checked\":" + ischeck + "");
                                        buff.Append("},");
                                    }

                                    //string roleright = "";
                                    //ZY.BLL.Tab_RolePower rolepower = new ZY.BLL.Tab_RolePower();
                                    ////获取角色对象
                                    //List<ZY.Model.Tab_RolePower> rolepowerList = rolepower.GetModelList("rid=" + request["rid"].ToString());
                                    //if (rolepowerList.Count > 0)
                                    //{
                                    //    roleright = rolepowerList[0].rp_powerContext.ToString();

                                    //}



                                    string temp = buff.ToString().TrimEnd(',') + "]}";
                                    HttpContext.Current.Response.ContentType = "text/plain";
                                    HttpContext.Current.Response.Write(temp);
                                    
                                }
                                else {
                                   HttpContext.Current.Response.ContentType = "text/plain";
                                   HttpContext.Current.Response.Write(JsonDataForRoleRight());
                                    

                                }
                            }

                        #endregion
                            break;
                        case "880":
                            break;
                        case "773":
                            break;
                        default:
                            break;
                    }

                }
                else {
                    Pubs.AjaxHandle.AjaxPress("300", "请求非法");
                   
                }



            }
            catch (Exception ex)
            {
                Pubs.AjaxHandle.AjaxPress("300", ex.Message);
                Helper.LogHelper.Writelog(ex.Message);
            }
            


        }


        private string JsonDataForRoleRight()
        {
            StringBuilder sbuff = new StringBuilder();
            ZY.BLL.Tab_Power powerdal = new ZY.BLL.Tab_Power();
            DataTable dspower = powerdal.GetAllList().Tables[0];
            sbuff.Append("{\"treedata\":[");
            foreach (DataRow item in dspower.Rows)
            {
                sbuff.Append("{");
                sbuff.Append("\"id\":" + item["po_id"].ToString() + ",");
                sbuff.Append("\"pId\":" + item["po_upid"].ToString() + ",");
                sbuff.Append("\"name\":\"" + item["po_name"].ToString() + "\",");
                sbuff.Append("\"open\":false,");
                sbuff.Append("\"checked\":false");
                sbuff.Append("},");
            }

            return  sbuff.ToString().TrimEnd(',') + "]}";
        }



    

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}